<template>

    <body id="poster">
        <h1>智慧农场大数据平台</h1>
        <el-form :rules="rules" ref="loginForm" class="login-container bgc" :model="loginForm" label-position="left"
            label-width="0px">
            <h3 class="login_title">
                系统登录
            </h3>
            <el-form-item prop="uname">
                <el-input class="input-bgc" type="text" v-model="loginForm.uname" placeholder="用户名"
                    autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="upwd">
                <el-input class="input-bgc" type="password" v-model="loginForm.upwd" placeholder="密码"
                    autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="Login" class="CreateButton">登录</el-button>
            </el-form-item>
        </el-form>
    </body>
</template>

<script>
export default {
    name: 'Login',
    data() {
        return {
            loading: false,
            loginForm: {
                uname: '',
                upwd: ''
            },
            rules: {
                uname: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
                upwd: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
            },
            token: ''
        }
    },
    methods: {
        Login() {
            // 验证表单元素的用户名和密码是否符合rules规则
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.loading = true
                    console.log(this.loginForm.uname, this.loginForm.upwd)
                    console.log(this.$apiBaseURL)
                    const requrl = this.$apiBaseURL + '/login?' + 'uname=' + this.loginForm.uname + '&' + 'upwd=' + this.loginForm.upwd
                    //axios访问后端
                    this.axios({
                        url: requrl,
                        methods: 'get'
                    }).then((response) => {
                        console.log("requrl：" + requrl, "--------------", response)
                        if (response.data !== '') {
                            this.$notify({
                                showClose: true,
                                duration: 1500,
                                title: '成功',
                                type: 'success',
                                message: '恭喜你，成功登录！'
                            });
                            this.token = response
                            localStorage.setItem('token', response.data)
                            this.$router.push({ path: '/Layout' })
                            this.loginForm = {};
                            this.loading = false
                        } else {
                            this.$notify({
                                showClose: true,
                                duration: 2000,
                                title: '错误',
                                type:'error',
                                message: '用户名或密码错误,请重新输入！'
                            });
                            this.loginForm = {};
                            this.loading = false
                        }
                    }).catch(error => {
                        console.log("error: " + error)
                    })
                } else {
                    this.$message({
                        message: '用户名或密码不能为空！',
                        type: 'warning'
                    });
                    this.loading = false
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped lang="less">
#poster {
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
    margin: 0px;
    padding: 0px;
    background-image: url(../../img/login-bg-earth.png);
    background-color: rgba(10, 18, 48, 1);

    h1 {
        margin-top: 125px !important;
        color: white;
        font-size: 50px;
        text-align: center;
    }

    .login-container {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 75px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;

        h3 {
            color: white;
        }
    }

    .bgc {
        background-color: rgba(10, 18, 49, 0.5);
    }
}


.login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
}

.CreateButton {
    width: 100%;
    background-color: rgb(9, 85, 157);
    border: none;
}
</style>